package com.zhengshang.api.service.impl;

import java.util.List;

import com.zhengshang.api.domain.AppBalanceRecord;
import com.zhengshang.api.mapper.AppBalanceRecordMapper;
import com.zhengshang.common.constant.app.AppConstant;
import com.zhengshang.common.core.domain.entity.SysUser;
import com.zhengshang.common.utils.DateUtils;
import com.zhengshang.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhengshang.api.mapper.AppCashingRecordMapper;
import com.zhengshang.api.domain.AppCashingRecord;
import com.zhengshang.api.service.IAppCashingRecordService;
import org.springframework.transaction.annotation.Transactional;

/**
 * APP用户提现记录Service业务层处理
 * 
 * @author zhengshang
 * @date 2021-03-30
 */
@Service
public class AppCashingRecordServiceImpl implements IAppCashingRecordService 
{
    @Autowired
    private AppCashingRecordMapper appCashingRecordMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private AppBalanceRecordMapper appBalanceRecordMapper;

    /**
     * 查询APP用户提现记录
     * 
     * @param id APP用户提现记录ID
     * @return APP用户提现记录
     */
    @Override
    public AppCashingRecord selectAppCashingRecordById(Long id)
    {
        return appCashingRecordMapper.selectAppCashingRecordById(id);
    }

    @Override
    public int queryTodayCount(AppCashingRecord appCashingRecord) {
        return appCashingRecordMapper.queryTodayCount(appCashingRecord);
    }

    /**
     * 查询APP用户提现记录列表
     * 
     * @param appCashingRecord APP用户提现记录
     * @return APP用户提现记录
     */
    @Override
    public List<AppCashingRecord> selectAppCashingRecordList(AppCashingRecord appCashingRecord)
    {
        return appCashingRecordMapper.selectAppCashingRecordList(appCashingRecord);
    }

    /**
     * 新增APP用户提现记录
     * 
     * @param appCashingRecord APP用户提现记录
     * @return 结果
     */
    @Override
    public int insertAppCashingRecord(AppCashingRecord appCashingRecord)
    {
        appCashingRecord.setCreateTime(DateUtils.getNowDate());
        return appCashingRecordMapper.insertAppCashingRecord(appCashingRecord);
    }

    /**
     * 修改APP用户提现记录
     * 
     * @param appCashingRecord APP用户提现记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateAppCashingRecord(AppCashingRecord appCashingRecord)
    {
        SysUser user = userMapper.selectUserById(appCashingRecord.getUserId());
        if ("2".equals(appCashingRecord.getCashingState())) {
            appCashingRecord.setPassTime(DateUtils.getNowDate());
            appCashingRecord.setRejectReason(null);
            //审核同意，已提现金额增加
            user.setAppWithdrawalMoney(user.getAppWithdrawalMoney().add(appCashingRecord.getCashingMoney()));
        } else if ("3".equals(appCashingRecord.getCashingState())) {
            //审核拒绝，增加余额变动记录
            AppBalanceRecord appBalanceRecord = new AppBalanceRecord();
            appBalanceRecord.setUserId(appCashingRecord.getUserId());
            appBalanceRecord.setMoney(appCashingRecord.getCashingMoney());
            appBalanceRecord.setModifyType(AppConstant.BALANCE_ADD);
            appBalanceRecord.setModifySource(AppConstant.MODIFY_SOURCE_WITHDRAWAL_REJECT);
            appBalanceRecord.setModifyAfterBalance(user.getAppAccountBalance());
            appBalanceRecord.setCreateTime(DateUtils.getNowDate());
            appBalanceRecord.setCreateBy(appCashingRecord.getUpdateBy());
            appBalanceRecordMapper.insertAppBalanceRecord(appBalanceRecord);

            //审核拒绝，余额增加
            appCashingRecord.setRejectTime(DateUtils.getNowDate());
            user.setAppAccountBalance(user.getAppAccountBalance().add(appCashingRecord.getCashingMoney()));
        } else {
            return 0;
        }
        //冻结金额减去提现的金额
        user.setAppFrozenAmount(user.getAppFrozenAmount().subtract(appCashingRecord.getCashingMoney()));
        userMapper.updateUser(user);
        appCashingRecord.setUpdateTime(DateUtils.getNowDate());
        return appCashingRecordMapper.updateAppCashingRecord(appCashingRecord);
    }

    /**
     * 批量删除APP用户提现记录
     * 
     * @param ids 需要删除的APP用户提现记录ID
     * @return 结果
     */
    @Override
    public int deleteAppCashingRecordByIds(Long[] ids)
    {
        return appCashingRecordMapper.deleteAppCashingRecordByIds(ids);
    }

    /**
     * 删除APP用户提现记录信息
     * 
     * @param id APP用户提现记录ID
     * @return 结果
     */
    @Override
    public int deleteAppCashingRecordById(Long id)
    {
        return appCashingRecordMapper.deleteAppCashingRecordById(id);
    }
}
